package com.xyzh05.webapp.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.Editor;
import com.xyzh05.model.Order;
import com.xyzh05.model.Pricing;
import com.xyzh05.model.Service;
import com.xyzh05.model.Transaction;
import com.xyzh05.model.TransactionEntry;
import com.xyzh05.model.VipService;
import com.xyzh05.service.EditorManager;
import com.xyzh05.service.PricingManager;
import com.xyzh05.service.ServiceManager;
import com.xyzh05.service.TransactionEntryManager;
import com.xyzh05.service.TransactionManager;
import com.xyzh05.service.VipServiceManager;
import com.xyzh05.util.PageBean;

public class TransactionEntryAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = 5282013520591393742L;
	
	private TransactionEntryManager transactionEntryManager;
    private TransactionManager transactionService;
    private VipServiceManager vipServiceManager;
    private EditorManager editorManager; 
    private ServiceManager serviceManager;
    private PricingManager pricingManager;
    private List<TransactionEntry> transactionEntries;
    private TransactionEntry transactionEntry;
    private PageBean pageBean;
    private VipService vipService;
    private List<Service> serviceList;
    private Long id;
    private Long editorId;
    private Long serviceId;
    private String query;
	private Long vipServiceId;
	private Integer resultCode;
	private String balance;
    

	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    }

    public String list() {
		vipService = vipServiceManager.get(vipServiceId);
    	pageBean = transactionEntryManager.findList(pageBean, transactionEntry ,vipServiceId);
    	return SUCCESS; 
    }

    public String delete() {
        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            transactionEntry = transactionEntryManager.get(id);
        } else {
            transactionEntry = new TransactionEntry();
        }
        return SUCCESS;
    }

    public String save() {
    	balance = transactionEntryManager.getBalance(vipServiceId);
    	if( 0 > new Double(balance)){
    		resultCode = 4;
    		return SUCCESS; 
    	}
    	
        try {
			List<Order> orderList = new ArrayList<Order>();
			Editor editor = editorManager.get(editorId);
			Service service =serviceManager.get(serviceId); 
			if(!editor.isServed(service.getServiceType())){
				resultCode = 3;
			}else{
				Pricing pricing = pricingManager.getPByEditorAService(editor.getUser().getId(), serviceId);
				Order order = new Order(editor, pricing);
				order.setSchoolNum( Transaction.START);
				orderList.add(order);
				Transaction transaction = transactionEntryManager.checkout(getCurrentUser(), orderList, Transaction.START ,vipServiceId );
				
				if (transaction == null){
					resultCode = 2;
				}else{
					resultCode = 1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultCode = 0;
		}
    	return SUCCESS; 
    }
    
    public String initAddOrder(){
    	try {
			balance = transactionEntryManager.getBalance(vipServiceId);
			serviceList = serviceManager.getByEditor();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return SUCCESS; 
    }
    

	public TransactionEntryManager getTransactionEntryManager() {
		return transactionEntryManager;
	}

	public void setTransactionEntryManager(
			TransactionEntryManager transactionEntryManager) {
		this.transactionEntryManager = transactionEntryManager;
	}

	public TransactionManager getTransactionService() {
		return transactionService;
	}

	public void setTransactionService(TransactionManager transactionService) {
		this.transactionService = transactionService;
	}

	public EditorManager getEditorManager() {
		return editorManager;
	}

	public void setEditorManager(EditorManager editorManager) {
		this.editorManager = editorManager;
	}

	public ServiceManager getServiceManager() {
		return serviceManager;
	}

	public void setServiceManager(ServiceManager serviceManager) {
		this.serviceManager = serviceManager;
	}

	public PricingManager getPricingManager() {
		return pricingManager;
	}

	public void setPricingManager(PricingManager pricingManager) {
		this.pricingManager = pricingManager;
	}

	public List<TransactionEntry> getTransactionEntries() {
		return transactionEntries;
	}

	public void setTransactionEntries(List<TransactionEntry> transactionEntries) {
		this.transactionEntries = transactionEntries;
	}

	public TransactionEntry getTransactionEntry() {
		return transactionEntry;
	}

	public void setTransactionEntry(TransactionEntry transactionEntry) {
		this.transactionEntry = transactionEntry;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getEditorId() {
		return editorId;
	}

	public void setEditorId(Long editorId) {
		this.editorId = editorId;
	}

	public Long getServiceId() {
		return serviceId;
	}

	public void setServiceId(Long serviceId) {
		this.serviceId = serviceId;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public List<Service> getServiceList() {
		return serviceList;
	}

	public void setServiceList(List<Service> serviceList) {
		this.serviceList = serviceList;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public VipServiceManager getVipServiceManager() {
		return vipServiceManager;
	}

	public void setVipServiceManager(VipServiceManager vipServiceManager) {
		this.vipServiceManager = vipServiceManager;
	}

	public VipService getVipService() {
		return vipService;
	}

	public void setVipService(VipService vipService) {
		this.vipService = vipService;
	}

	public Long getVipServiceId() {
		return vipServiceId;
	}

	public void setVipServiceId(Long vipServiceId) {
		this.vipServiceId = vipServiceId;
	}

	public String getBalance() {
		return balance;
	}

	public void setBalance(String balance) {
		this.balance = balance;
	}
    
}